package com.hr.dao;

import com.hr.model.Archive;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface ArchiveDAO
{

    @Results(id="archive",value = {
            @Result(id = true, property = "id", column = "a_id"),
            @Result(property = "name", column = "a_name"),
            @Result(property = "sex", column = "a_sex"),
            @Result(property = "mail", column = "a_mail"),
            @Result(property = "intro", column = "a_brief"),
            @Result(property = "date", column = "a_time"),
            @Result(property = "pub", column = "a_public"),
            @Result(property = "tel", column = "a_tel"),
            @Result(property = "policy", column = "a_policy"),
            @Result(property = "area", column = "a_area"),
            @Result(property = "degree", column = "a_degree"),
            @Result(property = "school", column = "a_school"),
            @Result(property = "major", column = "a_major"),
            @Result(property = "age", column = "a_age"),
            // list
            @Result(property = "talents", column = "a_id", many = @Many(select = "com.hr.dao.TalentDAO.getTalentsByAId")),
            @Result(property = "positions", column = "a_id", many = @Many(select = "com.hr.dao.PositionDAO.getPositionsByAId")),
            @Result(property = "deeds", column = "a_id", many = @Many(select = "com.hr.dao.DeedDAO.getDeedsByAId")),
            @Result(property = "evaluates", column = "a_id", many = @Many(select = "com.hr.dao.EvaluateDAO.getEvaluatesByAId")),
    })
    @Select("select * from archive where a_id=#{aid}")
    Archive getArchiveByAId(@Param("aid")int aid);

    @ResultMap("archive")
    @Select("select * from archive where u_id=#{uid}")
    Archive getArchiveByUId(@Param("uid")int uid);

    @Transactional
    @ResultMap("archive")
    @Update("update archive set " +
            "a_id=#{archive.id}, " +
            "a_name=#{archive.name}," +
            "a_sex=#{archive.sex}, " +
            "a_mail=#{archive.mail}, " +
            "a_brief=#{archive.intro}, " +
            "a_time=#{archive.date}, " +
            "a_public=#{archive.pub}, " +
            "a_tel=#{archive.tel}, " +
            "a_policy=#{archive.policy}, " +
            "a_area=#{archive.area}, " +
            "a_degree=#{archive.degree}, " +
            "a_school=#{archive.school}, " +
            "a_major=#{archive.major}, " +
            "a_age=#{archive.age}")
    void updateArchive(@Param("archive")Archive archive);

//    @ResultMap("archive")
//    @Insert("INSERT into archive (u_id,a_name,a_sex,a_mail,a_brief,a_time,a_public,a_tel,a_policy,a_area,a_degree,a_school,a_major) " +
//            "VALUES (#{uid},#{archive.name},#{archive.sex},#{archive.mail},#{archive.brief},#{archive.time},#{archive.public},#{archive.tel},#{archive.policy},#{archive.area},#{archive.degree},#{archive.school},#{archive.major})")
//    void insertS_tById_S_ts(@Param("archive")Archive archive,@Param("uid")int uid);

    @ResultMap("archive")
    @Select("select * from archive")
    List<Archive> getAllArchives();

    @ResultMap("archive")
    @Select("select * from archive where a_public is true")
    List<Archive> getPublicArchives();

    @ResultMap("archive")
    @Select("select * from department join position p on department.dep_id = p.dep_id join archive a on p.a_id = a.a_id where department.dep_id =#{depid}")
    List<Archive> getArchivesByDepId(@Param("depid")int depid);

    @SelectKey(keyProperty = "archive.id",before = false,resultType = Integer.class,statement = "select last_insert_id()")
    @Insert("INSERT into archive (u_id,a_name,a_sex,a_mail,a_brief,a_time,a_public,a_tel,a_policy,a_area,a_degree,a_school,a_major,a_age) " +
            "VALUES (#{uid},#{archive.name},#{archive.sex},#{archive.mail},#{archive.intro},#{archive.date},#{archive.pub},#{archive.tel},#{archive.policy},#{archive.area},#{archive.degree},#{archive.school},#{archive.major},#{archive.age})")
    void insertArchiveByUId(@Param("uid")int uid, @Param("archive")Archive archive);

    @Update("update archive set " +
            "a_name=#{archive.name},a_sex=#{archive.sex},a_mail=#{archive.mail},a_brief=#{archive.intro},a_time=#{archive.date}," +
            "a_public=#{archive.pub},a_tel=#{archive.tel},a_policy=#{archive.policy},a_area=#{archive.area},a_degree=#{archive.degree}, " +
            "a_school=#{archive.school},a_major=#{archive.major},a_age=#{archive.age} where a_id=#{aid}")
    void updateArchiveByAId(@Param("aid")int aid, @Param("archive")Archive archive);
}
